<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="咨询公司" prop="companyId">
        <el-select
          v-model="queryParams.companyId"
          clearable
          @keyup.enter.native="handleQuery"
          filterable
          placeholder="请输入或选择咨询公司"
        >
          <el-option
            v-for="item in company"
            :key="item.companyId"
            :label="item.companyName"
            :value="item.companyId"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="受理人" prop="userId">
        <el-select
          v-model="queryParams.userId"
          clearable
          @keyup.enter.native="handleQuery"
          filterable
          placeholder="请输入或选择受理人"
        >
          <el-option
            v-for="item in acceptor"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="咨询时间" prop="adviceTime">
        <el-date-picker
          clearable
          v-model="queryParams.adviceTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择咨询时间"
          :style="{ width: '90%' }"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="回复时间" prop="replyTime">
        <el-date-picker
          clearable
          v-model="queryParams.replyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择回复时间"
          style="width: 160px"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:advice:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:advice:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-badge :value="noReplyNumber" :max="10">
          <el-button
            size="mini"
            icon="el-icon-more"
            type="primary"
            plain
            @click="handleReply"
            >未回复</el-button
          >
        </el-badge>
      </el-col>
      <audio
        controls="controls"
        hidden
        src="../voice/zhendong.mp3"
        ref="audio1"
      ></audio>
      <audio
        controls="controls"
        hidden
        src="../voice/tips.mp3"
        ref="audio2"
      ></audio>
      <audio
        controls="controls"
        hidden
        src="../voice/delete.mp3"
        ref="audio3"
      ></audio>
      <audio
        controls="controls"
        hidden
        src="../voice/Download.mp3"
        ref="audio4"
      ></audio>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="adviceList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" width="55" />
      <el-table-column
        label="咨询公司"
        align="center"
        prop="companyName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="咨询类型" align="center" prop="dictLabel" />
      <el-table-column
        label="咨询内容"
        align="center"
        :show-overflow-tooltip="true"
        prop="adviceContent"
      />
      <el-table-column label="受理人" align="center" prop="nickName" />
      <el-table-column
        label="咨询时间"
        align="center"
        prop="adviceTime"
        width="180"
        sortable
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.adviceTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="回复内容"
        align="center"
        :show-overflow-tooltip="true"
        prop="replyContent"
      />
      <el-table-column
        label="回复时间"
        align="center"
        prop="replyTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.replyTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="180px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:advice:edit']"
            >回复</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:advice:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleLookOver(scope.row)"
            v-hasPermi="['system:7:query']"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      class="maskpopup"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="咨询公司" prop="companyId">
          <el-select
            v-model="form.companyId"
            filterable
            placeholder="请输入或选择咨询公司"
          >
            <el-option
              v-for="item in company"
              :key="item.companyId"
              :label="item.companyName"
              :value="item.companyId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="咨询类型" prop="adviceType">
          <el-select
            v-model="form.adviceType"
            filterable
            placeholder="请选择咨询类型"
          >
            <el-option
              v-for="dict in dict.type.advice_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="咨询内容" prop="adviceContent">
          <el-input
            v-model="form.adviceContent"
            :min-height="192"
            type="textarea"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 修改对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="opens"
      width="500px"
      class="maskpopup"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="回复内容" prop="replyContent">
          <el-input
            v-model="form.replyContent"
            :min-height="192"
            type="textarea"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 查看详情弹窗 -->
    <el-dialog
      :title="title"
      :visible.sync="lookOver"
      width="500px"
      class="maskpopup"
      append-to-body
    >
      <el-form ref="form" :model="form" label-width="120px" size="mini">
        <el-row>
          <el-col :span="22">
            <el-form-item label="咨询公司：">{{
              form.companyName
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="咨询类型：">{{ form.dictLabel }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="咨询内容：">{{
              form.adviceContent
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="咨询时间：">{{
              form.adviceTime
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="受理人：">{{ form.nickName }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="回复内容：">{{
              form.replyContent
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="回复时间：">{{ form.replyTime }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAdvice,
  getAdvice,
  delAdvice,
  addAdvice,
  updateAdvice,
  listUser,
  getNoReplyAdvice,
} from "@/api/system/advice";
import { companyList } from "@/api/system/company";

export default {
  name: "Advice",
  dicts: ["advice_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //未回复消息数量
      noReplyNumber: 0,
      // 纳税咨询表格数据
      adviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹窗
      open: false,
      opens: false,
      lookOver: false,
      //公司下拉列表
      company: [],
      //受理人下拉列表
      acceptor: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        companyName: null,
        adviceType: null,
        dictLabel: null,
        adviceContent: null,
        userId: null,
        nickName: null,
        adviceTime: null,
        replyContent: null,
        replyTime: null,
        fakeDelete: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyId: [
          {
            required: true,
            message: "咨询公司不能为空",
            trigger: "change",
          },
        ],
        adviceType: [
          {
            required: true,
            message: "咨询类型不能为空",
            trigger: "change",
          },
        ],
        adviceContent: [
          {
            required: true,
            message: "咨询内容不能为空",
            trigger: "change",
          },
        ],
        replyContent: [
          {
            required: true,
            message: "回复内容不能为空",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.getCompany();
    this.getAcceptor();
    this.handle();
  },
  methods: {
    //公司列表
    getCompany() {
      companyList(this.queryParams).then((response) => {
        this.company = response.rows;
      });
    },
    //受理人列表
    getAcceptor() {
      listUser(this.queryParams).then((response) => {
        this.acceptor = response.rows;
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listAdvice(this.queryParams).then((response) => {
        this.adviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查看并回复未回复的消息 */
    handleReply() {
      this.loading = true;
      getNoReplyAdvice(this.queryParams).then((response) => {
        this.adviceList = response.rows;
        this.loading = false;
      });
    },
    handle() {
      getNoReplyAdvice(this.queryParams).then((response) => {
        this.noReplyNumber = response.total;
        if (this.noReplyNumber != 0) {
          this.$refs.audio1.currentTime = 0; //从头开始播放提示音
          this.$refs.audio1.play(); //播放
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.opens = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        adviceId: null,
        companyId: null,
        adviceType: null,
        adviceContent: null,
        userId: null,
        adviceTime: null,
        replyContent: null,
        replyTime: null,
        fakeDelete: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.adviceId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加咨询";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.audio2.currentTime = 0;
      this.$refs.audio2.play();
      this.reset();
      const adviceId = row.adviceId || this.ids;
      getAdvice(adviceId).then((response) => {
        this.form = response.data;
        this.opens = true;
        this.title = "修改咨询";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.adviceId != null) {
            updateAdvice(this.form).then((response) => {
              this.$modal.msgSuccess("该咨询已回复");
              this.opens = false;
              this.getList();
              this.handle();
            });
          } else {
            addAdvice(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.handle();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$refs.audio3.currentTime = 0;
      this.$refs.audio3.play();
      const adviceIds = row.adviceId || this.ids;
        this.$modal
        .confirm('是否确认删除序号为"' + adviceIds + '"的咨询？')
        .then(() => {
          delAdvice(adviceIds).then((res)=>{
              console.log("////"+res);
            if(res!='101'){
              this.getList();
          this.$modal.msgSuccess("该条咨询已成功删除");
            }else{
              this.$modal.msgWarning("该条咨询未回复，不可删除")
            }
          });
        })
        .catch(() => {});
    },
    /** 查看详情 */
    handleLookOver(row) {
      this.$refs.audio2.currentTime = 0;
      this.$refs.audio2.play();
      getAdvice(row.adviceId).then((response) => {
        this.form = response.data;
        this.lookOver = true;
        this.title = "查看咨询详情";
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/advice/export",
        {
          ...this.queryParams,
        },

        `advice_${new Date().getTime()}.xlsx`
      );
      this.$refs.audio4.currentTime = 0;
      this.$refs.audio4.play();
    },
  },
};
</script>
